package ttOrder.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ttOrder.service.IDinnertableService;
import ttOrder.vo.CommonMessageResultVO;
import ttOrder.vo.DinnertableVO;

@Controller
public class DinnertableController {

	@Autowired
	public IDinnertableService dinnertableService;
	
	//查询全部
	@RequestMapping("/showAllDinnertable")
	public ModelAndView showAllDinnertable(ModelAndView mv){
		mv.addObject("dinnertables",dinnertableService.findAllDinnertable());
		mv.setViewName("adminDinnertable/showAllDinnertable");
		return mv;
	}
	
	//显示全部空闲餐桌
	@RequestMapping("/showAllFreeTable")
	public ModelAndView showAllFreeTable(ModelAndView mv){
		mv.addObject("FreeTables",dinnertableService.showAllFreeTable());
		mv.setViewName("adminDinnertable/showFreeTable");
		return mv;
	}
	
	
	
	
	//新增
	@ResponseBody
	@RequestMapping(value="/saveDinnertable",method = RequestMethod.POST)
	public CommonMessageResultVO saveDinnertable(@RequestBody DinnertableVO dinnertableVO,BindingResult bindingResult){
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String, String> messageMap = new HashMap<String,String>();
		resultVO.setSuccess(false);
		if(bindingResult.hasErrors()){
			List<ObjectError> errorList = bindingResult.getAllErrors();
			for(ObjectError error : errorList){
				FieldError fe = (FieldError) error;
				messageMap.put(fe.getField(), fe.getDefaultMessage());
			}
			resultVO.setMessageMap(messageMap);
			return resultVO;
		}
		
		try {
			dinnertableService.insertDinnertable(dinnertableVO);
			resultVO.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
	}
	
	//根据ID显示餐桌信息	
	@ResponseBody
	@RequestMapping(value="/getDinnertableById",method = RequestMethod.POST)
	public DinnertableVO findDinnertableById(Integer tableId){		
		DinnertableVO	resultVO = dinnertableService.findDinnertableById(tableId);
		return resultVO;
	
	}
		
	
	//服务员根据餐桌状态查询餐桌信息
	@RequestMapping(value="/showBytableStatus",method=RequestMethod.POST)
	public String showBytableStatus(Model model,Integer tableStatus){
		List<DinnertableVO> resultVO=dinnertableService.showBytableStatus(tableStatus);
		model.addAttribute("showBytableStatus",resultVO);
		return "AdminDinnerTable/showBytableStatus";
	}

	/*//查询全部
		@RequestMapping("/showAllDinnertable")
		public ModelAndView showAllDinnertable(ModelAndView mv){
			mv.addObject("dinnertables",dinnertableService.findAllDinnertable());
			mv.setViewName("adminDinnertable/showAllDinnertable");
			return mv;
		}*/
	
	
	//修改餐桌
	@ResponseBody
	@RequestMapping(value="/editDinnertable",method = RequestMethod.POST)
	public CommonMessageResultVO editFood(@RequestBody DinnertableVO dinnertableVO,BindingResult bindingResult){
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String, String> messageMap = new HashMap<String,String>();
		resultVO.setSuccess(false);
		if(bindingResult.hasErrors()){
			List<ObjectError> errorList = bindingResult.getAllErrors();
			for(ObjectError error : errorList){
				FieldError fe = (FieldError) error;
				messageMap.put(fe.getField(), fe.getDefaultMessage());
			}
			resultVO.setMessageMap(messageMap);
			return resultVO;
		}
		try {
			dinnertableService.updateDinnertable(dinnertableVO);
			resultVO.setSuccess(true);
		} catch (Exception e) {
			
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
	}
	
	//删除 餐桌
	@RequestMapping(value="/deleteDinnertable",method=RequestMethod.POST)
	@ResponseBody
	public boolean deleteFood(Integer tableId){	
		dinnertableService.deleteDinnertable(tableId);	
		return true;
	}
	
}
